package com.alt.ui;

import com.alt.R;
import com.alt.base.ALTBaseActivity;
import com.alt.handler.CMCmdHandler;
import com.alt.manager.ALTAppMgr;
import com.alt.manager.ALTPTCMgr;
import com.alt.manager.ALTPTCMgr.SCREEN_UI;
import com.alt.manager.ALTPTCMgr.ValidateUserResponse;
import com.alt.models.AccessDeviceInfo;
import com.alt.shared.IActivityHandler;
import com.alt.utils.ALTSysUtils;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ALTRegister extends ALTBaseActivity implements IActivityHandler {

	private EditText edtUsername, edtPassword, edtPassword2;
	private Button btnRegister;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_register);
		onSetScreenMode(SCREEN_UI.LOGIN);

		iniControls();

	}

	private void iniControls() {
		edtUsername = (EditText) findViewById(R.id.edEmail);
		edtPassword = (EditText) findViewById(R.id.edPass);
		edtPassword2 = (EditText) findViewById(R.id.edPass2);
		btnRegister = (Button) findViewById(R.id.btnRegister);

		btnRegister.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// validate input data
				if (checkConstraint()) {
					// service process
					Bundle cmdData = new Bundle();
					cmdData.putString("username", String.valueOf(edtUsername.getText()));
					cmdData.putString("password", String.valueOf(edtPassword.getText()));
					cmdData.putString("deviceid", AccessDeviceInfo.getDeviceID(getApplicationContext()));
					ALTAppMgr.getInstance().startServiceCmd(CMCmdHandler.COMMAND_REGISTER_NEW_ACCOUNT, new CMCmdHandler(), cmdData, true);
				}
			}
		});

	}

	public Boolean checkConstraint() {

		if (this.edtUsername.getText().toString().trim().equalsIgnoreCase("")) {
			alert("Vui lòng nhập email đăng nhập");
			return false;
		}

		if (!(Boolean) ALTSysUtils.isValidateEmail(this.edtUsername.getText().toString())) {
			alert("Email không chính xác!");
			return false;
		}

		if (this.edtPassword.getText().toString().trim().equalsIgnoreCase("")) {
			alert("Vui lòng nhập mật khẩu");
			return false;
		}
		if (!this.edtPassword.getText().toString().trim().equalsIgnoreCase(this.edtPassword2.getText().toString().trim())) {
			alert("Mật khẩu không trùng");
			return false;
		}

		return true;
	}

	@Override
	public void onScreenChanged(SCREEN_UI curScreen) {
		super.onScreenChanged(curScreen);
		finish();
	}

	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		super.processCommandResult(commandCode, resultData);

		Bundle bResult = (Bundle) resultData;

		switch (commandCode) {

		case CMCmdHandler.COMMAND_REGISTER_NEW_ACCOUNT: {
			String result = bResult.getString(ALTPTCMgr.REST_OBJECT_RETURN);
			result = result.replace("\"", "");
			if (result.equalsIgnoreCase(ValidateUserResponse.DUPLICATED_ACCOUNT.toString())) {
				alert(getString(R.string.msg_duplicated_account));
			} else if (result.equalsIgnoreCase(ValidateUserResponse.DUPLICATED_DEVICE.toString())) {
				alert(getString(R.string.msg_duplicated_device));
			} else if (result.equalsIgnoreCase(ValidateUserResponse.SUCCESS.toString())) {
				AlertDialog.Builder alert = new AlertDialog.Builder(this.getParent() == null ? this : this.getParent());
				alert.setMessage(R.string.msg_register_success);
				alert.setPositiveButton(R.string.txt_btn_ok, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						move2login();
					}
				});
				alert.show();
			} else {
				alert(getString(R.string.msg_register_fail));
			}

			// onEventReceiver(SCREEN_UI.MAIN);
		}
			break;

		default:
			break;

		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		move2login();
	}

	private void move2login() {
		Intent i = new Intent(this, ALTLogin.class);
		startActivity(i);
	}

}